#include <iostream>

using namespace std;

int sequence(int a,int b,int n)
{

	switch(n)
	{
		case 1:return 1;
		case 2:return 1;
		default:
			int at1=1,at2=1,temp=0;
			for(int i=3;i<=n;i++)
			{
				temp=at1;
				at1=(a*at1+b*at2)%7;
				at2=temp;
			}
			return at1;

	}
}

int main()
{
	int a,b,n;
    while(cin>>a>>b>>n)
	{
		if(a==0&&b==0&&n==0)
			break;
		cout<<sequence(a,b,n%49)<<endl;
	}
	return 0;
}
